package com.my.lucky.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Slf4j
@Aspect
@Component
public class ExecutionTimeAspect {
    /**
     * 判断是否是禁止执行时间
     *
     * @return
     */
    public static boolean issideRestrictedTime() {
        LocalDateTime now = LocalDateTime.now();
        DayOfWeek day = now.getDayOfWeek();
        LocalTime time = now.toLocalTime();

        // 检查是否是周一到周五
        boolean isWeekday = day.getValue() >= DayOfWeek.MONDAY.getValue()
                && day.getValue() <= DayOfWeek.FRIDAY.getValue();

        if (!isWeekday) {
            return false; // 周末直接返回false
        }

        // 检查是否在9:10-11:35之间
        boolean inMorningRange = time.isAfter(LocalTime.of(9, 10))
                && time.isBefore(LocalTime.of(11, 35));

        // 检查是否在12:58-15:05之间
        boolean inAfternoonRange = time.isAfter(LocalTime.of(12, 58))
                && time.isBefore(LocalTime.of(15, 5));

        // 如果不在任何限制时间段内则返回true
        return (inMorningRange || inAfternoonRange);
    }

    //    @Around("execution(* com.my.lucky.service.IWencaiServiceTools.*(..))")
//    @Around("execution(* com.my.lucky.service.IWencaiServiceTools.*(..))")
    public Object logTime(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = null;
        if (issideRestrictedTime()) {
            result = pjp.proceed();
        } else {
            log.info(pjp.getSignature() + "\texecuted=out\t");
        }
        long duration = System.currentTimeMillis() - start;
        log.info(pjp.getSignature() + "\texecuted=in\t" + duration + "\tms");
        return result;
    }

}
